package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.DictPO;
import com.falcon.domain.dos.DictDO;
import com.falcon.domain.req.DictQuery;
import com.falcon.domain.req.DictRequest;

import com.falcon.mapper.IDictMapper;
import com.falcon.service.IDictService;


@Slf4j
@Service
public class DictService implements IDictService {

    @Autowired
    private IDictMapper dictMapper;

    @Override
    public Long save(DictRequest request){
        DictPO dictPO = BeanUtil.copyProperties(request, DictPO.class);

        this.dictMapper.insertNotNull(dictPO);

        return dictPO.getId();
    }

    @Override
    public Boolean update(Long id, DictRequest request){

        DictPO dictPO = BeanUtil.copyProperties(request, DictPO.class);
        dictPO.setId(id);

        return this.dictMapper.updateNotNull(dictPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.dictMapper.deleteById(id);
    }

    @Override
    public DictDO queryById(Long id){

        DictPO dictPO = this.dictMapper.selectById(id);

        DictDO dictDO = BeanUtil.copyProperties(dictPO, DictDO.class);

        return dictDO;
    }


    @Override
    public List<DictDO> queryList(DictQuery request){

        Condition condition = request.toCondition();

        List<DictPO> poList = this.dictMapper.selectList(condition);

        List<DictDO> doList = BeanUtil.copyToList(poList, DictDO.class);

        return doList;
    }

    @Override
    public BasePager<DictDO> queryPager(DictQuery request){


        PageInfo<DictPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.dictMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, DictDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.dictMapper.deleteByIds(ids);
    }


}
